<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 6.3.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon.jpg">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon.jpg">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"matian.connorma.cn","root":"/","scheme":"Muse","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="在 Spring Boot 中观察了一下有新的请求时，Servlet filter 的执行情况，做下记录。 Spring Boot 默认 Servlet filterSpring Boot 会按照 ApplicationFilterChain 中的数组 filters 里保存的 filter 按顺序执行，对于 OrderedFilter 可以指定 order 值（一个 int 值），值小的先执行，">
<meta property="og:type" content="article">
<meta property="og:title" content="Servlet filter记录">
<meta property="og:url" content="http://matian.connorma.cn/bfd4b596.html">
<meta property="og:site_name" content="马天">
<meta property="og:description" content="在 Spring Boot 中观察了一下有新的请求时，Servlet filter 的执行情况，做下记录。 Spring Boot 默认 Servlet filterSpring Boot 会按照 ApplicationFilterChain 中的数组 filters 里保存的 filter 按顺序执行，对于 OrderedFilter 可以指定 order 值（一个 int 值），值小的先执行，">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2019-03-19T05:54:17.000Z">
<meta property="article:modified_time" content="2024-01-25T12:19:17.206Z">
<meta property="article:author" content="马天">
<meta property="article:tag" content="java">
<meta property="article:tag" content="Spring Boot">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://matian.connorma.cn/bfd4b596.html">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>Servlet filter记录 | 马天</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">马天</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://matian.connorma.cn/bfd4b596.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="马天">
      <meta itemprop="description" content="程序猿">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="马天">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Servlet filter记录
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2019-03-19 13:54:17" itemprop="dateCreated datePublished" datetime="2019-03-19T13:54:17+08:00">2019-03-19</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Spring/" itemprop="url" rel="index"><span itemprop="name">Spring</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>在 Spring Boot 中观察了一下有新的请求时，Servlet filter 的执行情况，做下记录。</p>
<h2 id="Spring-Boot-默认-Servlet-filter"><a href="#Spring-Boot-默认-Servlet-filter" class="headerlink" title="Spring Boot 默认 Servlet filter"></a>Spring Boot 默认 Servlet filter</h2><p>Spring Boot 会按照 ApplicationFilterChain 中的数组 filters 里保存的 filter 按顺序执行，对于 OrderedFilter 可以指定 order 值（一个 int 值），值小的先执行，不指定 order 时按照首字母顺序执行。</p>
<p>默认情况下，有 CharacterEncodingFilter、HiddenHttpMethodFilter、FormContentFilter 和 RequestContextFilter，以及 webSocket 的 WsFilter。CharacterEncodingFilter、HiddenHttpMethodFilter、FormContentFilter 和 RequestContextFilter 都是使用了 Ordered***Filter 实例，order 值分别是 -2147483648、-10000、-9900 和 -105。</p>
<p>RequestContextFilter 主要用于使用第三方 Servlet 时的情况。关于 CharacterEncodingFilter、HiddenHttpMethodFilter 和 FormContentFilter，其作用可见如下。 </p>
 <span id="more"></span>

<ol>
<li>CharacterEncodingFilter</li>
</ol>
<p>用于指定 request 的编码方式。如果 request 没有指定编码方式，或者是 CharacterEncodingFilter 指定了 forceEncoding &#x3D; true，会添加（覆盖）request 的编码方式，具体来说，其操作是在 header 的 Content-Type 中添加 charset 项。</p>
<p>同时，如果指定了 forceEncoding &#x3D; true，也会在 response 的 header 的 Content-Type 中添加 charset 项，虽然对这一操作，文档提示”although this will usually be overridden by a full content type set in the view”。</p>
<ol start="2">
<li>HiddenHttpMethodFilter</li>
</ol>
<p>form 表单只能使用 GET 和 POST 方法，这一个过滤器可以包装为 PUT、DELETE 和 PATCH 方法。</p>
<p>使用时，需要在 form 表单中指定 method 为 POST，同时添加一个 key 为 “_method” 的属性，指定为实际要使用的方法（PUT、DELETE 或 PATCH），HiddenHttpMethodFilter 即可包装为该 method。这一属性默认 key 为”_method”，也可以指定为其他值。</p>
<p>另，文档提示，这一 filter 要在 multipart 处理过后执行，所以如果自定义了 MultipartFilter，MultipartFilter 应指定在 HiddenHttpMethodFilter 前执行。</p>
<ol start="3">
<li>FormContentFilter</li>
</ol>
<p>对 PUT, PATCH 和 DELETE 的方法传递的参数进行包装，使得可以像 POST 请求一样，可以作为 Servlet request parameters 来读取。</p>
<h2 id="其他常用-filter"><a href="#其他常用-filter" class="headerlink" title="其他常用 filter"></a>其他常用 filter</h2><p>以后补充这部分吧…</p>
<h2 id="Spring-Boot-自定义-filter"><a href="#Spring-Boot-自定义-filter" class="headerlink" title="Spring Boot 自定义 filter"></a>Spring Boot 自定义 filter</h2><h3 id="WebFilter-注解"><a href="#WebFilter-注解" class="headerlink" title="@WebFilter 注解"></a>@WebFilter 注解</h3><p>是 Servlet 3 中定义的注解，用于声明一个 Servlet filter，urlPatterns 属性可指定匹配的 url 的列表。使用时需要添加 @ServletComponentScan 才会生效，@ServletComponentScan 需要指定扫描的包或者类，可以直接添加在 @SpringBootApplication 类之上使扫描生效。</p>
<h3 id="实现-Filter-接口添加一个-filter"><a href="#实现-Filter-接口添加一个-filter" class="headerlink" title="实现 Filter 接口添加一个 filter"></a>实现 Filter 接口添加一个 filter</h3><ol>
<li>使用 @WebFilter 声明，实现 Filter 接口</li>
</ol>
<p>以下代码将新注册一个 filter，同时需要在 @SpringBootApplication 类下添加 @ServletComponentScan 使之生效。</p>
<p>@WebFilter 注解的文档提醒，”At least one URL pattern MUST be declared in either the value or urlPattern attribute of the annotation, but not both”。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@WebFilter</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">MyFilter</span> <span class="keyword">implements</span> <span class="title class_">Filter</span> &#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">doFilter</span><span class="params">(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)</span> <span class="keyword">throws</span> IOException, ServletException &#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;MyFilter&quot;</span>);</span><br><span class="line">        filterChain.doFilter(servletRequest, servletResponse);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>要记得调用 filterChain.doFilter() 方法，来“Causes the next filter in the chain to be invoked”。</p>
<p>这一方法无法指定顺序（实现 OrderedFilter 接口也不能，？）。</p>
<ol start="2">
<li>使用 @Component 声明，实现 Filter 接口，并使用 @Order 指定顺序</li>
</ol>
<p>不需要指定 @WebFilter 中的属性时，也可以使用 @Component 来声明以注册一个新的 filter。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Order(-9901)</span></span><br><span class="line"><span class="meta">@Component</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">MyFilter</span> <span class="keyword">implements</span> <span class="title class_">Filter</span> &#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">doFilter</span><span class="params">(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)</span> <span class="keyword">throws</span> IOException, ServletException &#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;MyFilter&quot;</span>);</span><br><span class="line">        filterChain.doFilter(servletRequest, servletResponse);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>如上，将添加一个 MyFilter，并且其执行顺序在默认的 FormContentFilter 之前（FormContentFilter 默认 order 为 -9900）。</p>
<ol start="3">
<li>使用 @Component 声明，实现 OrderedFilter 接口指定顺序</li>
</ol>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Component</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">MyFilter</span> <span class="keyword">implements</span> <span class="title class_">OrderedFilter</span> &#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">doFilter</span><span class="params">(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)</span> <span class="keyword">throws</span> IOException, ServletException &#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;MyFilter&quot;</span>);</span><br><span class="line">        filterChain.doFilter(servletRequest, servletResponse);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> <span class="type">int</span> <span class="title function_">getOrder</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> -<span class="number">9901</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>如上，将添加一个 MyFilter，并且其执行顺序在默认的 FormContentFilter 之前（FormContentFilter 默认 order 为 -9900），效果与使用 @Order(-9901) 相同。</p>
<h3 id="继承某个-Filter-类来替换默认实现或者添加一个-filter"><a href="#继承某个-Filter-类来替换默认实现或者添加一个-filter" class="headerlink" title="继承某个 ***Filter 类来替换默认实现或者添加一个 filter"></a>继承某个 ***Filter 类来替换默认实现或者添加一个 filter</h3><p>以继承 FormContentFilter 为例。</p>
<ol>
<li>使用 @WebFilter 声明，继承 FormContentFilter</li>
</ol>
<p>代码如下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@WebFilter</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">MyFilter</span> <span class="keyword">extends</span> <span class="title class_">FormContentFilter</span> &#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">protected</span> <span class="keyword">void</span> <span class="title function_">doFilterInternal</span><span class="params">(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)</span> <span class="keyword">throws</span> ServletException, IOException &#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;MyFilter&quot;</span>);</span><br><span class="line">        filterChain.doFilter(servletRequest, servletResponse);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>与“使用 @WebFilter 声明，实现 Filter 接口”类似，新注册一个 FormContentFilter，无法指定顺序。同样需要在 @SpringBootApplication 类下添加 @ServletComponentScan 使之生效。</p>
<ol start="2">
<li>使用 @Component 声明，继承 FormContentFilter，并使用 @Order 指定顺序</li>
</ol>
<p>对如下代码，MyFilter 将直接作为一个 bean，替换掉 Spring Boot 中 FormContentFilter 的默认实现的 bean。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Component</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">MyFilter</span> <span class="keyword">extends</span> <span class="title class_">FormContentFilter</span> &#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">protected</span> <span class="keyword">void</span> <span class="title function_">doFilterInternal</span><span class="params">(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)</span> <span class="keyword">throws</span> ServletException, IOException &#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;MyFilter&quot;</span>);</span><br><span class="line">        filterChain.doFilter(request, response);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>只使用 @Component 声明一个继承自 FormContentFilter 的类，此自定义 filter 将替换掉默认的 FormContentFilter，但是顺序是默认顺序，而不是原 FormContentFilter 的顺序（查看 Spring Boot 的 WebMvcAutoConfiguration 类可以看到，原 FormContentFilter 是实例化了一个 OrderedFormContentFilter，order 为 -9900）。</p>
<p>如果使用 @Order 来指定顺序，如下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Order(-9901)</span></span><br><span class="line"><span class="meta">@Component</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">MyFilter</span> <span class="keyword">extends</span> <span class="title class_">FormContentFilter</span> &#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">protected</span> <span class="keyword">void</span> <span class="title function_">doFilterInternal</span><span class="params">(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)</span> <span class="keyword">throws</span> ServletException, IOException &#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;MyFilter&quot;</span>);</span><br><span class="line">        filterChain.doFilter(request, response);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>可以看到 @Order(-9901) 生效，MyFilter 按照 -9901 的值应有的顺序放到 filterChain 中。</p>
<h3 id="自定义多个-filter-时的执行顺序"><a href="#自定义多个-filter-时的执行顺序" class="headerlink" title="自定义多个 filter 时的执行顺序"></a>自定义多个 filter 时的执行顺序</h3><p>使用 @WebFilter 声明的 filter，可以指定 urlPatterns，将作为一个新的 filter 加入到 filterChain 中，无法指定其执行顺序，需要使用 @ServletComponentScan 扫描使之生效。</p>
<p>使用 @Component 注册的 filter，可以使用 @Order 指定顺序或者实现 OrderedFilter 接口指定顺序。</p>
<p>此外，还可以通过在代码中定义 bean 的方式，注册一个 FilterRegistrationBean，来定义一个 filter。</p>

    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/java/" rel="tag"># java</a>
              <a href="/tags/Spring-Boot/" rel="tag"># Spring Boot</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/1d720228.html" rel="prev" title="令人惊叹的HashMap—数据结构与hash函数">
      <i class="fa fa-chevron-left"></i> 令人惊叹的HashMap—数据结构与hash函数
    </a></div>
      <div class="post-nav-item">
    <a href="/9a904cd8.html" rel="next" title="java动态代理：java-Proxy和CGLIB">
      java动态代理：java-Proxy和CGLIB <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#Spring-Boot-%E9%BB%98%E8%AE%A4-Servlet-filter"><span class="nav-number">1.</span> <span class="nav-text">Spring Boot 默认 Servlet filter</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%85%B6%E4%BB%96%E5%B8%B8%E7%94%A8-filter"><span class="nav-number">2.</span> <span class="nav-text">其他常用 filter</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Spring-Boot-%E8%87%AA%E5%AE%9A%E4%B9%89-filter"><span class="nav-number">3.</span> <span class="nav-text">Spring Boot 自定义 filter</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#WebFilter-%E6%B3%A8%E8%A7%A3"><span class="nav-number">3.1.</span> <span class="nav-text">@WebFilter 注解</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%AE%9E%E7%8E%B0-Filter-%E6%8E%A5%E5%8F%A3%E6%B7%BB%E5%8A%A0%E4%B8%80%E4%B8%AA-filter"><span class="nav-number">3.2.</span> <span class="nav-text">实现 Filter 接口添加一个 filter</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%BB%A7%E6%89%BF%E6%9F%90%E4%B8%AA-Filter-%E7%B1%BB%E6%9D%A5%E6%9B%BF%E6%8D%A2%E9%BB%98%E8%AE%A4%E5%AE%9E%E7%8E%B0%E6%88%96%E8%80%85%E6%B7%BB%E5%8A%A0%E4%B8%80%E4%B8%AA-filter"><span class="nav-number">3.3.</span> <span class="nav-text">继承某个 ***Filter 类来替换默认实现或者添加一个 filter</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%87%AA%E5%AE%9A%E4%B9%89%E5%A4%9A%E4%B8%AA-filter-%E6%97%B6%E7%9A%84%E6%89%A7%E8%A1%8C%E9%A1%BA%E5%BA%8F"><span class="nav-number">3.4.</span> <span class="nav-text">自定义多个 filter 时的执行顺序</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">马天</p>
  <div class="site-description" itemprop="description">程序猿</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">41</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">17</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">25</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/matian2014" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;matian2014" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:matian1103@qq.com" title="E-Mail → mailto:matian1103@qq.com" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        
  <div class="beian"><a href="https://beian.miit.gov.cn/" rel="noopener" target="_blank">陕ICP备2023001563号 </a>
  </div>

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2025</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">马天</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://muse.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/muse.js"></script>


<script src="/js/next-boot.js"></script>




  















  

  

</body>
</html>
